$(document).ready(function() {
    $('#submitBill').click(function() {
      var formData = {
        remark: $('#remarks').val(),
        debt_date: $('#debt-date').val(),
        goods: $('#goods').val(),
        cargo_quantity: $('#cargo-quantity').val(),
        price: $('#price').val(),
        total_arrears: $('#total-arrears').val()
      };
  
      $.ajax({
        url: '/bill',  // 请确保这是正确的后端路由
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify(formData),
        success: function(response) {
          console.log('收到响应:', response);
          if (response && response.success) {
            alert('账单添加成功');
            $('#AddBillModal').modal('hide');
            // 刷新整个页面
            location.reload();
          } else {
            alert('错误: ' + (response && response.msg ? response.msg : '未知错误'));
          }
        },
        error: function(xhr, status, error) {
          console.error('AJAX 错误:', status, error);
          console.error('响应文本:', xhr.responseText);
          alert('发生错误: ' + error);
        }
      });
    });
  });
